# -*- coding: utf-8 -*-
# @Date：2024/7/19 22:57:00
# @Author：CJiaの青姝
# @FileName：captcha
# @Editor：PyCharm2024
# @Remark：
import base64
from io import BytesIO
from datetime import timedelta

from ninja import Router

from utils.cj_captcha import vieCode
from utils.cj_response import SuccessResponse

router = Router()


@router.get('/captcha', auth=None, summary="获取验证码")
def captcha(request):
    image, code = vieCode().GetCodeImage()
    code = ''.join(code).lower()
    out = BytesIO()
    request.session["code"] = code
    request.session.set_expiry(timedelta(minutes=1))
    image.save(out, format='png')
    out.seek(0)
    img_base64 = base64.b64encode(out.getvalue()).decode("utf-8")
    result = {
        "image_base": "data:image/png;base64," + img_base64,
    }
    return SuccessResponse(data=result, msg="获取成功")
